<!DOCTYPE html>
<html lang="zh-cn">

<head>
    <meta charset="utf-8">
    <title>徐翔yyds</title>
    <script>
        function copyFn() {
            let text = document.getElementById("copyMsg").innerText;
            let input = document.getElementById("input");
            input.value = text; // 将div中需要复制的文本拷贝进textarea
            input.select(); // 选中文本
            document.execCommand("copy"); // 执行浏览器复制命令
            alert("复制成功");
        }
    </script>
    <style>
        #input {
            position: absolute;
            top: 0;
            left: 0;
            opacity: 0;
            z-index: -10;
        }
    </style>
</head>

<body>
<pre>
<?php
function trimMultibyte($string, $charlist = '\\\\s', $ltrim = true, $rtrim = true) {
    $both_ends = $ltrim && $rtrim;

    $char_class_inner = preg_replace(
        array('/[\^\-\]\\\]/S', '/\\\{4}/S'),
        array('\\\\\\0', '\\'),
        $charlist
    );

    $work_horse = '[' . $char_class_inner . ']+';
    $ltrim && $left_pattern = '^' . $work_horse;
    $rtrim && $right_pattern = $work_horse . '$';

    if ($both_ends) {
        $pattern_middle = $left_pattern . '|' . $right_pattern;
    } elseif ($ltrim) {
        $pattern_middle = $left_pattern;
    } else {
        $pattern_middle = $right_pattern;
    }

    return preg_replace("/$pattern_middle/usSD", '', $string);
}

function formatStr($s): string {
    $s = str_replace("\r\n", "\n", $s);
    $s = str_replace("\"", '\"', $s);
    return str_replace('\'', "\'", $s);
}

function scriptStr($noticeType, $title, $detail, $time, $url, $startTime): string {
    global $NOTICE_NAME;

    $title = trimMultibyte($title, "【】");
//    $midStr = $noticeType ?
//        "活动类型：$NOTICE_NAME[$noticeType]\n开始时间：$time" :
//        "ddl：$time";
    $midStr = $noticeType ?
        "活动类型：$NOTICE_NAME[$noticeType]\n" : "";
    $midStr .= $startTime? "开始时间：$startTime\nDDL:$time" : "DDL:$time";
    return "【{$title}】\n$midStr\n$detail\nDDL系统链接：$url";
}


/*
 * 常量：
 * MD5KEY: 各单位密码的 md5sum
 * $PARTY_ID: 各单位在 class2 接口的编号
 * $NOTICE_NAME: 各活动的实际名称
 *   PARTY_ID 和 NOTICE_NAME 实际没有声明为常量，因为后面不会写
 */
const MD5KEY = [
    "dddd" => "270b85586c36c685c098fcd7a41aaf73",
    "304"  => "4de04b47b57a1a8247f845abd89f5b0f",
    "305"  => "5a7dd969f0283b6a9813bc243b91762b",
    "306"  => "e91321f73dee3823e4165bd7a8abf8bd",
    "307"  => "d1a802815b9216f774cf42e92f93cb00",
    "308"  => "1e26600c6e9dcc06b71ba01f8d9c5545",
    "309"  => "9e850532883621bf2987cae121e94390",
    "test" => "c4d038b4bed09fdb1471ef51ec3a32cd", // password := 114514
];
$PARTY_ID = [
    "dddd" => "1",
    "304"  => "2",
    "305"  => "7",
    "306"  => "8",
    "307"  => "9",
    "308"  => "10",
    "309"  => "11",
    "test" => "6",
];
$NOTICE_NAME = [
    "0" => "DDL",
    "1" => "活动",
    "2" => "思政活动",
    "3" => "文体活动",
    "4" => "志愿活动",
    "5" => "讲座",
    "6" => "竞赛",
    "7" => "评优",
];

/*
 * $key: 密码
 * $title: 标题
 * $detailRaw: 详情，换行符全为 \n
 * $detailFormatted: 详情，换行符经 "<br>" 替换
 * $noticeType: 通知类型，暂 0~6(str)
 * $ddlTime: 时间到分钟，例 "2021-11-26 20:10"
 * $ddlTime2Sec: 时间到秒，例 "2021-11-26 20:10:00"
 * $startTime:开始时间到分钟
 * $startTime2Sec:开始时间到秒
 * $class: 操作对应的表
 * $url: 新通知链接到的地址 f"http://www.squidward.top/[cla=...][#...]"
 *   "dddd" => "http://www.squidward.top/index.php"
 *   "30x" => "http://www.squidward.top/index.php?cla=30x"
 */
$key             = $_POST["key"];
$title           = formatStr($_POST["title"]);
$detailRaw       = formatStr($_POST["detail"]);
$detailFormatted = str_replace("\n", "<br>", $detailRaw); // replace \n with <br>
$noticeType      = $_POST["noticeType"]; // 新前端的 noticeType 改成了 <select> 标签递值，于是删去了原先的判断
$ddlTime         = str_replace("T", " ", $_POST["ddl"]);
$startTime         = str_replace("T", " ", $_POST["startTime"]);
$ddlTime2Sec     = $ddlTime . ":00";
$startTime2Sec     = $startTime . ":00";
$class           = $_POST["classes"];
$url             = "http://www.squidward.top/" . (strncmp($class, "30", 2) ? "" : ("?cla=" . $class));
$echoStr         = "\n\n----------\n以下是命令回显\n\n";


if (md5($key) != MD5KEY["$class"]) {
    die("Success!");
}


try {
//  $connPdo = new PDO("mysql:host=127.0.0.1:3306;dbname=test;charset=utf8", "root", "");
    $connPdo = new PDO('mysql:host=localhost;dbname=test;charset=utf8', 'squidward_top', '789456');
    $connPdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);


    // 原谅这么丑的写法，但是这样就不用写注释了（
    if( !strstr($startTime2Sec,"0000-00-00 00:00:00")){
        $statement =
        "INSERT INTO test.$class SET\n
    title = \"$title\",
    detail = \"$detailRaw\",
    ddl = \"$ddlTime2Sec\",
    noticeType = \"$noticeType\"
    ";}
    else{
        $statement =
        "INSERT INTO test.$class SET\n
    title = \"$title\",
    detail = \"$detailRaw\",
    ddl = \"$ddlTime2Sec\",
    startTime = \"$startTime2Sec\",
    noticeType = \"$noticeType\"
    ";
    }
    
    $returnValue = $connPdo->exec($statement);
    $echoStr .= "$statement\n\nwith PDO return value $returnValue\n\n";

    $index_ = $connPdo->lastInsertID();
    $url .= "#$index_";

    $command =
        "../class2 $PARTY_ID[$class] $noticeType \"$title\" \"$startTime--$ddlTime\"  \"$detailFormatted\" \"$url\"";
    system($command, $returnValue);
    $echoStr .= "$command\n\nwith system return value $returnValue\n\n";


    $statement =
        "SELECT *
 FROM test.$class\n
 WHERE index_ = $index_
 LIMIT 1";
    foreach ($connPdo->query($statement) as $row) {
        // 下面的 <textarea> 标签仅用来帮助复制，由 css 设置为不可见
        echo
            '<button onClick="copyFn()">点击复制</button><br>' .
            '<box id="copyMsg">' .
            scriptStr($row["noticeType"], $row["title"], $row["detail"], $row["ddl"],$url,$row["startTime"]) .
            '</box>' .
            '<textarea id="input">这里仅用来帮助复制</textarea>';
    }

    echo $echoStr;
} catch (PDOException $e) {
    die("die 了，以下回显是人为输出的 =>\n{$e->getMessage()}");
}
?>
</pre>
</body>
